package com.chenhy.springbootvuesmbms.controller;

import com.chenhy.springbootvuesmbms.pojo.Bill;
import com.chenhy.springbootvuesmbms.pojo.CommonResult;
import com.chenhy.springbootvuesmbms.pojo.Provider;
import com.chenhy.springbootvuesmbms.service.BillService;
import com.chenhy.springbootvuesmbms.service.ProviderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.annotation.Secured;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/bill")
public class BillController {

    @Autowired
    private BillService billService;

    @Autowired
    private ProviderService providerService;

    @GetMapping("/billList")
    public CommonResult<Object[]> queryAllBill(){
        List<Bill> bills = billService.queryAllBill();
        return new CommonResult<Object[]>(200,"查询成功",bills.toArray());
    }

    @GetMapping("/fuzzyQueryBill")
    public CommonResult<List<Bill>> fuzzyBillQuery(String condition, Model model){
        List<Bill> bills = billService.fuzzyBillQuery(condition);
        return new CommonResult<List<Bill>>(200,"查询成功",bills);
    }


    @PostMapping("/addBill")
    public CommonResult addBill(@RequestBody Bill bill){
        billService.addBill(bill);
        return CommonResult.success();
    }

    @GetMapping("/billDetails")
    public String toBillDetails(int id, Model model){
        Bill bill = billService.queryBillById(id);
        model.addAttribute("bill",bill);
        return "bill/billDetails";
    }

    @GetMapping("/updateBill")
    public String toUpdateBill(int id, Model model){
        Bill bill = billService.queryBillById(id);
        List<Provider> providers = providerService.queryAllProvider();
        model.addAttribute("providerList", providers);
        model.addAttribute("bill", bill);
        return "bill/updateBill";
    }

    @PostMapping("/updateBill")
    public String updateBill(Bill bill){
        billService.updateBill(bill);
        return "bill/updateBill";
    }

    @PostMapping("/deleteBill")
    public String deleteBill(int id){
        billService.deleteBill(id);
        return "redirect:billList";
    }


}
